package com.gdqy.FCS.interceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor {
	
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
	//拦截请求
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		
		HttpSession session = request.getSession();
		String uri = request.getRequestURI();
		//如果用户请求登录、注册，放行
		if(uri.indexOf("deal_login.do")>0 
				|| uri.indexOf("login.do")>0
				|| uri.indexOf("deal_register.do")>0
				||uri.indexOf("register.do")>0
				||uri.indexOf("verifyUsername.do")>0
				||uri.indexOf("verifyEmail.do")>0
				||uri.indexOf("validateEmail.do")>0
				|| uri.indexOf("index.do")>0
				){
			return true;
		}//其他页面用户是否登录
		else{
			if(session.getAttribute("username") != null){
				return true;
			}
		}
		
		request.getRequestDispatcher("/WEB-INF/views/common/login.jsp").forward(request, response);
		
		return false;
	}

}
